package cn.juque.workflow.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.workflow.api.dto.leaveinfo.LeaveInfoListDTO;
import cn.juque.workflow.api.dto.leaveinfo.LeaveInfoSearchDTO;
import cn.juque.workflow.api.provider.ILeaveInfoProvider;
import cn.juque.workflow.entity.LeaveInfo;
import cn.juque.workflow.service.ILeaveInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>请假申请单信息</li>
 * </ul>
 * @date 2023-01-31 18:14:44
 **/
@Slf4j
@Service("leaveInfoProvider")
@HandlerAppException
public class LeaveInfoProviderImpl implements ILeaveInfoProvider {

    @Resource
    private ILeaveInfoService leaveInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<LeaveInfoListDTO>> list(LeaveInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        LeaveInfo leaveInfo = new LeaveInfo();
        BeanUtil.copyProperties(searchDTO, leaveInfo);
        List<LeaveInfo> infoList;
        if (null == pageInfo) {
            infoList = this.leaveInfoService.listInfoWithoutPage(leaveInfo);
        } else {
            infoList = this.leaveInfoService.listInfoWithPage(pageInfo, leaveInfo);
        }
        List<LeaveInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, LeaveInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }
}
